package com.stone.back.user.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.stone.back.user.entity.BfCountry;
import com.stone.back.user.entity.BfUser;
import com.stone.back.user.service.IUserService;
import com.stone.base.controller.BaseController;
import com.stone.base.entity.Page;
import com.stone.common.util.Md5Util;

/**
 * 后台用户管理
 * @author Stone P
 *
 */
@Controller
@RequestMapping("/backUserCtl")
public class UserController extends BaseController{
	@Autowired
	private IUserService userService;

	/**
	 * 后台用户登录
	 * @param email		账户名称
	 * @param password			密码
	 * @return
	 */
	@RequestMapping("/backUserLogin")
	public @ResponseBody boolean backUserLogin(String email, String password, HttpServletRequest request) {
		boolean loginStatus = false;
		if(StringUtils.isNotBlank(email) && StringUtils.isNotBlank(password)) {
			password = Md5Util.toMD5(password);
			BfUser user = this.userService.userLogin(email, password);
			if(null != user){
				request.getSession().setAttribute(this.BACKUSER, user);
				loginStatus = true;
			}
		}
		return loginStatus;
	}

	/**
	 * 后台用户注销
	 * @return
	 */
	@RequestMapping("/userLogout")
	public ModelAndView userLogout(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/backLogin");
		request.getSession().removeAttribute(this.BACKUSER);
		return mv;
		
	}
	
	/**
	 * 查询用户分页信息
	 * @param pageNumber			页数
	 * @param userName				用户名
	 * @param email					email
	 * @param sex					性别
	 * @param type0:普通 1:后台)		用户类型
	 * @return
	 */
	@RequestMapping("/select")
    public @ResponseBody Page selectUserPage(String pageNumber, String userName, String email, String sex, String type, HttpServletRequest request) {
		if(StringUtils.isBlank(pageNumber)) {
			pageNumber = "1";
		}
		Page page = this.userService.selectUserPage(Integer.parseInt(pageNumber), userName, email, sex, type);
		return page;
	}
	
	/**
	 * 验证登录名是否存在
	 * @param email		登录名
	 * @return
	 */
	@RequestMapping("/checkEmailName")
    public @ResponseBody int selectUserByEmail(String email, HttpServletRequest request) {
		int count = this.userService.selectUserByEmail(email);
		return count;
	}
	
	/**
	 * 查询国家列表
	 * @return
	 */
	@RequestMapping("/selectCountry")
    public @ResponseBody List<BfCountry> selectCountry(HttpServletRequest request) {
		List<BfCountry> list = this.userService.selectCountry();
		return list;
	}
	
	/**
	 * 增加/修改用户
	 * @param user	用户信息
	 * @return
	 */
	@RequestMapping("/add")
    public ModelAndView addUser(BfUser user, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/background/user/userlist");
		try {
			BfUser currentUser = this.getBackCurrentUser(request);
			user.setCreatedate(new Date());
			user.setPassword(Md5Util.toMD5(user.getPassword()));
			user.setCreateuserid(currentUser.getUserid());
			if(null == user.getUserid()) {
				this.userService.addUser(user);
			} else {
				this.userService.updateUser(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	/**
	 * 获取用户信息去修改
	 * @param user	用户信息
	 * @return
	 */
	@RequestMapping("/toUpdate")
    public ModelAndView toUpdateUser(int userId, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/background/user/useradd");
		BfUser user = this.userService.selectUserDetail(userId);
		mv.addObject("user", user);
		return mv;
	}
	
	/**
	 * 查询用户详情
	 * @param userId	用户Id
	 * @return
	 */
	@RequestMapping("/detail")
    public ModelAndView selectUserDetail(int userId, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/background/user/userdetail");
		BfUser user = this.userService.selectUserDetail(userId);
		mv.addObject("user", user);
		return mv;
	}
	
	/**
	 * 删除用户
	 * @param userId	用户Id
	 * @return
	 */
	@RequestMapping("/delete")
    public @ResponseBody boolean deleteUser(int userId, HttpServletRequest request) {
		this.userService.deleteUser(userId);
		return true;
	}
}
